﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS
{
    #region Using Statements

    using System.Text;
    using System.Collections.Generic;
    using KLF.Lib.Browser.DOM.CSS.SAC.Selectors;

    #endregion

    /// <summary>
    /// The CSSStyleRule interface represents a single rule set in a CSS 
    /// style sheet.
    /// </summary>
    public class CSSStyleRule : CSSRule
    {
        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        internal CSSStyleRule()
        {
            selectorText = "";
            style = new CSSStyleDeclaration();
            Selectors = new List<Selector>(); 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_style">The parent style containing this property.</param>
        internal CSSStyleRule(CSSStyleDeclaration _style)
            : this()
        {
            style = _style;
        }

        #endregion

        #region Attributes

        internal List<Selector> Selectors { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Adds a selector to the rule.
        /// </summary>
        /// <param name="s"></param>
        internal void AddSelector(Selector s)
        {
            Selectors.Add(s);
        }

        #endregion

        #region DOM Level 2 CSS Compliance

        /// <summary>
        /// The textual representation of the selector for the rule set. The 
        /// implementation may have stripped out insignificant whitespace
        /// while parsing the selector.
        /// </summary>
        public string selectorText
        {
            get
            {
                return _selectorText != null ? _selectorText.Value : "";
            }
            set
            {
                _selectorText = new StringLookup(value);
            }
        }
        internal StringLookup _selectorText;

        /// <summary>
        /// The declaration-block of this rule set.
        /// </summary>
        public CSSStyleDeclaration style
        {
            get;
            internal set;
        }

        #endregion

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach (Selector s in Selectors)
            {
                if (sb.Length > 0)
                    sb.Append(", ");
                sb.Append(s.ToString());
            }

            sb.Append("{\n");
            if (style != null)
            {
                sb.Append(style.ToString());
            }
            sb.Append("}\n");

            return sb.ToString();
        }
    }
}
